public class Test02 {

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    long prev = Long.MIN_VALUE;

    //二叉搜索树中序遍历是有序的
    public boolean isValidBST(TreeNode root) {
        //空树也是二叉搜索树
        if (root == null) {
            return true;
        }
        //中序遍历
        //判断左子树
        boolean leftTree = isValidBST(root.left);
        //剪枝
        if (leftTree == false){
            return false;
        }
        //判断当前根节点
        //假设当前节点 为 fales
        boolean cur = false;
        if (root.val > prev) {
            cur = true;
        }
        //更新前驱
        prev = root.val;
        //判断左子树
        boolean rightTree = isValidBST(root.right);
        return leftTree && cur && rightTree;
    }

    public static void main(String[] args) {

    }
}
